package com.jv.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jv.entity.*;
import com.jv.service.IProductService;
import com.jv.util.DateUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员-商品控制器
 * @author Jv
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/product")
public class AdminProductController {

    @Autowired
    private IProductService productService;

    @Value("${productImagesFilePath}")
    private String productImagesFilePath;  //商品图片地址

    @Value("${swiperImagesFilePath}")
    private String swiperImagesFilePath;  //首页Swiper幻灯图地址

    //根据条件分页查询 商品
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean) {
        System.out.println(pageBean);
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", pageBean.getQuery().trim());
        map.put("start", pageBean.getStart());
        map.put("pageSize", pageBean.getPageSize());
        List<Product> productList = productService.list(map);
        Long total = productService.getTotals(map);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("productList", productList);
        resultMap.put("total", total);
        return R.ok(resultMap);
    }

    //更新热门状态
    @GetMapping("/updateHot/{id}/state/{hot}")
    public R updateHot(@PathVariable(value = "id")Integer id,@PathVariable(value = "hot") Boolean hot){
        Product product = productService.getById(id);
        product.setHot(hot);
        if (hot) {
            product.setHotDateTime(new Date());
        }else {
            product.setHotDateTime(null);
        }
        productService.saveOrUpdate(product);
        return R.ok();
    }

    //更新Swiper状态
    @GetMapping("/updateSwiper/{id}/state/{swiper}")
    public R updateSwiper(@PathVariable(value = "id")Integer id,@PathVariable(value = "swiper") Boolean swiper){
        Product product = productService.getById(id);
        product.setSwiper(swiper);
        productService.saveOrUpdate(product);
        return R.ok();
    }

    //删除商品
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id") Integer id){
        productService.removeById(id);
        return R.ok();
    }

    //上传商品图片
    @RequestMapping("/uploadImage")
    public Map<String ,Object> uploadImage(MultipartFile file)throws Exception{
        HashMap<String, Object> resultMap = new HashMap<>();
        if (!file.isEmpty()) {
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            //获取后缀  .jpg
            String suffixName=originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName= DateUtil.getCurrentDateStr()+suffixName;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(productImagesFilePath+newFileName));
            resultMap.put("code",0);
            resultMap.put("msg","上传成功");
            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","/image/product/"+newFileName);
            resultMap.put("data",dataMap);
        }
        return resultMap;
    }

    //添加或修改商品
    @RequestMapping("/save")
    public R save(@RequestBody Product product){
        if (product.getId()==null || product.getId()==-1) {//添加
            productService.add(product);
        }else { //修改
            productService.update(product);
        }
        return R.ok();
    }

    //上传Swiper幻灯图
    @RequestMapping("/uploadSwiperImage")
    public Map<String ,Object> uploadSwiperImage(MultipartFile file)throws Exception{
        HashMap<String, Object> resultMap = new HashMap<>();
        if (!file.isEmpty()) {
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            //获取后缀  .jpg
            String suffixName=originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName= DateUtil.getCurrentDateStr()+suffixName;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(swiperImagesFilePath+newFileName));
            resultMap.put("code",0);
            resultMap.put("msg","上传成功");
            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","/image/swiper/"+newFileName);
            resultMap.put("data",dataMap);
        }
        return resultMap;
    }

    //根据id查询
    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        Product product = productService.findById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("product",product);
        return R.ok(map);
    }
}